package com.lanou.musicbox.fragment.onlinemusic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.onlinemusic.EnterTheRankingMusicAdapter;
import com.lanou.musicbox.base.BaseFragment;
import com.lanou.musicbox.bean.RankingMusic;
import com.lanou.musicbox.custom.Events;
import com.lanou.musicbox.entity.Entity;
import com.lanou.musicbox.utils.Contant;
import com.lanou.musicbox.utils.VolleySingleton;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by 杨海宁 on 15/10/23.
 * 网络音乐_进入排行榜
 */
public class EnterTheRankingMusicFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {

    private ListView listView;
    private StringRequest stringRequest;
    private List<RankingMusic.SongListEntity> songListEntities;
    private VolleySingleton volleySingleton;
    private EnterTheRankingMusicAdapter adapter;
    private ImageView ranking_music_img, ranking_music_back;
    private TextView ranking_music_number;
    private ImageLoader imageLoader;
    private RankingMusic rankingMusic;
    private CollapsingToolbarLayout collapsingToolbarLayout;
    private String URl_type;
    private String URl_all;
    private String TAG = "lanou3344";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_ranking_music, null);
        listView = (ListView) v.findViewById(R.id.ranking_listview_music);
        listView.setCacheColorHint(1);
        ranking_music_img = (ImageView) v.findViewById(R.id.ranking_music_img);
        ranking_music_back = (ImageView) v.findViewById(R.id.ranking_music_back);
        collapsingToolbarLayout = (CollapsingToolbarLayout) v.findViewById(R.id.collapsing_toolbar);
        ranking_music_img = (ImageView) v.findViewById(R.id.ranking_music_img);
        ranking_music_number = (TextView) v.findViewById(R.id.ranking_music_number);
        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        imageLoader = volleySingleton.getImageLoader();
        collapsingToolbarLayout.setTitleEnabled(true);
        ranking_music_back.setOnClickListener(this);
        listView.setOnItemClickListener(this);
        //接收数据,获得 type_id 的值
        Bundle bundle = getArguments();
        URl_type = bundle.getString("type_id");
        initDatas();
    }

    //解析数据
    private void initDatas() {
        URl_all = Contant.Url.URl_EnterRanking_first + URl_type + Contant.Url.URl_EnterRanking_end;
        Log.i(TAG, "initDatas_URl_all " + URl_all);
        stringRequest = new StringRequest(URl_all, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Gson gson = new Gson();
                rankingMusic = gson.fromJson(s, RankingMusic.class);
                if (rankingMusic != null) {
                    songListEntities = rankingMusic.getSong_list();
                    Log.i(TAG, "onResponse " + songListEntities.size());
                    adapter = new EnterTheRankingMusicAdapter(songListEntities, getActivity());
                    initView();
                    listView.setAdapter(adapter);

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        volleySingleton.addQueue(stringRequest);
    }

    private void initView() {
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(ranking_music_img, R.mipmap.loading, R.mipmap.loading);
        imageLoader.get(rankingMusic.getBillboard().getPic_s260(), listener);
        ranking_music_number.setText(rankingMusic.getSong_list().size() + "");
        collapsingToolbarLayout.setTitle(rankingMusic.getBillboard().getName());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ranking_music_back:
                getFragmentManager().popBackStack();
                break;
        }
    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        List<Entity> songsInfos = new ArrayList<>();
        for (int i = 0; i < songListEntities.size(); i++) {
            Entity netSongs = new Entity();
            String song_id = songListEntities.get(i).getSong_id();
            String title = songListEntities.get(i).getTitle();
            String pic_small = songListEntities.get(i).getPic_small();
            String pic_premium = songListEntities.get(i).getPic_big();
            String author = songListEntities.get(i).getAuthor();
            //String Lrc_link = songListEntities.get(i).getLrclink();

            netSongs.setSongs_id(song_id);
            netSongs.setTitle(title);
            netSongs.setSmall_pic(pic_small);
            netSongs.setBig_pic(pic_premium);
            netSongs.setArtist(author);
           // netSongs.setSongLrcLink(Lrc_link);
            netSongs.setItem_id(position);
            songsInfos.add(i, netSongs);

        }

        String whatMusic = "网络";
        Events.MusicInfoEvent musicInfoEvent= new Events.MusicInfoEvent();
        musicInfoEvent.setMp3Infos(songsInfos,whatMusic);
        EventBus.getDefault().post(musicInfoEvent);

    }
}
